package com.inspur.gs.sbssdl.sbwhgl.sbwhby.sbwhby.front.byszxz_frm.voactions;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bff.spi.*;
import com.inspur.edp.bef.api.action.*;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class confirmVOAction extends AbstractFSAction<VoidActionResult> {

	private String[] ids;
	private Object[] datas;
	private String lx;
	public confirmVOAction(String[] ids, Object[] datas, String lx) {
		this.ids = ids;
		this.datas = datas;
		this.lx = lx;
	}

	private static String sbtzCfg = "efd56b39-0e8d-4441-9fa1-2b019b6fd862";

	@Override
	public void execute() {
		List<sbbyszRecord> bypzlist = new ArrayList<>();
		for (Object datum : datas) {
			sbbyszRecord record = parseRecord(datum.toString());
			bypzlist.add(record);
		}
		SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
		try {
			//点检配置变更
			ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
			String byid = "db558bb5-532f-430d-b610-13f9fc5835bd";// 设备点检配置id
			IStandardLcp byszlcp = iLcpFactory.createLcpByBEId(byid);

			//设备台账的变更
			IStandardLcp lcp = iLcpFactory.createLcpByBEId(sbtzCfg);

			for (String id : ids) {
				if (lx.equals("1")){
					IEntityData data = byszlcp.retrieve(id).getData();
					String sbid = (String) ((IValuesContainer)data.getValue("sbid")).getValue("sbid");

					ModifyChangeDetail modify = new ModifyChangeDetail(sbid);
					modify.putItem("ry", true);
					// 主表变更
					ModifyChangeDetail curChange = new ModifyChangeDetail(id);
					curChange.getPropertyChanges().put("ybpz","已配置");
					//子表变更
					for (sbbyszRecord record : bypzlist) {
						ArrayList<String> nodeCodes = new ArrayList();
						nodeCodes.add("ybgl");
						ArrayList<String> hierarchyIds = new ArrayList();
						hierarchyIds.add(id);
						IEntityData ybgldata = byszlcp.retrieveDefaultChild(nodeCodes, hierarchyIds);
						ModifyChangeDetail childChange = new ModifyChangeDetail(ybgldata.getID());
						childChange.getPropertyChanges().put("sbdl", record.getSbdl());
						childChange.getPropertyChanges().put("sbxl", record.getSbxl());
						childChange.getPropertyChanges().put("bydj", record.getBydj());
						childChange.getPropertyChanges().put("bybw", record.getBybw());
						childChange.getPropertyChanges().put("byxm", record.getByxm());
						childChange.getPropertyChanges().put("gxnr", record.getGxnr());
						childChange.getPropertyChanges().put("hgbz", record.getHgbz());
						curChange.addChildChangeSet("ybgl", childChange);
					}
					byszlcp.modify(curChange);
					byszlcp.save();
					lcp.modify(modify);
					lcp.save();
				} else if (lx.equals("2")) {
					IEntityData data = byszlcp.retrieve(id).getData();
					String sbid = (String) ((IValuesContainer)data.getValue("sbid")).getValue("sbid");

					ModifyChangeDetail modify = new ModifyChangeDetail(sbid);
					modify.putItem("re", true);
					// 主表变更
					ModifyChangeDetail curChange = new ModifyChangeDetail(id);
					curChange.getPropertyChanges().put("ebpz","已配置");
					//子表变更
					for (sbbyszRecord record : bypzlist) {
						ArrayList<String> nodeCodes = new ArrayList();
						nodeCodes.add("ebgl");
						ArrayList<String> hierarchyIds = new ArrayList();
						hierarchyIds.add(id);
						IEntityData ebgldata = byszlcp.retrieveDefaultChild(nodeCodes, hierarchyIds);
						ModifyChangeDetail childChange = new ModifyChangeDetail(ebgldata.getID());
						childChange.getPropertyChanges().put("sbdl", record.getSbdl());
						childChange.getPropertyChanges().put("sbxl", record.getSbxl());
						childChange.getPropertyChanges().put("bydj", record.getBydj());
						childChange.getPropertyChanges().put("bybw", record.getBybw());
						childChange.getPropertyChanges().put("byxm", record.getByxm());
						childChange.getPropertyChanges().put("gxnr", record.getGxnr());
						childChange.getPropertyChanges().put("hgbz", record.getHgbz());
						childChange.getPropertyChanges().put("fsff", record.getFsff());
						childChange.getPropertyChanges().put("gqj", record.getGqj());
						childChange.getPropertyChanges().put("degs", record.getDegs());
						curChange.addChildChangeSet("ebgl", childChange);
					}
					byszlcp.modify(curChange);
					byszlcp.save();
					lcp.modify(modify);
					lcp.save();
				}

			}
		} finally {
			SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
		}
	}


	public static class sbbyszRecord {
		private String sbdl;
		private String sbxl;
		private String bydj;
		private String bybw;
		private String byxm;
		private String gxnr;
		private String gy;
		private String hgbz;
		private String fsff;
		private String gqj;
		private String degs;

		public String getSbdl() {
			return sbdl;
		}

		public void setSbdl(String sbdl) {
			this.sbdl = sbdl;
		}

		public String getSbxl() {
			return sbxl;
		}

		public void setSbxl(String sbxl) {
			this.sbxl = sbxl;
		}

		public String getBydj() {
			return bydj;
		}

		public void setBydj(String bydj) {
			this.bydj = bydj;
		}

		public String getBybw() {
			return bybw;
		}

		public void setBybw(String bybw) {
			this.bybw = bybw;
		}

		public String getByxm() {
			return byxm;
		}

		public void setByxm(String byxm) {
			this.byxm = byxm;
		}

		public String getGxnr() {
			return gxnr;
		}

		public void setGxnr(String gxnr) {
			this.gxnr = gxnr;
		}

		public String getGy() {
			return gy;
		}

		public void setGy(String gy) {
			this.gy = gy;
		}

		public String getHgbz() {
			return hgbz;
		}

		public void setHgbz(String hgbz) {
			this.hgbz = hgbz;
		}

		public String getFsff() {
			return fsff;
		}

		public void setFsff(String fsff) {
			this.fsff = fsff;
		}

		public String getGqj() {
			return gqj;
		}

		public void setGqj(String gqj) {
			this.gqj = gqj;
		}

		public String getDegs() {
			return degs;
		}

		public void setDegs(String degs) {
			this.degs = degs;
		}
	}


	public static sbbyszRecord parseRecord(String recordStr) {
		String content = recordStr.substring(1, recordStr.length() - 1);
		String[] pairs = content.split(", ");

		Map<String, String> map = new HashMap<>();
		for (String pair : pairs) {
			String[] kv = pair.split("=", 2);
			if (kv.length == 2) {
				map.put(kv[0], kv[1].equals("null") ? null : kv[1]);
			}
		}

		sbbyszRecord record = new sbbyszRecord();
		try {
			record.setSbdl(map.get("sbdl"));
			record.setSbxl(map.get("sbxl"));
			record.setBydj(map.get("bydj"));
			record.setBybw(map.get("bybw"));
			record.setByxm(map.get("byxm"));
			record.setGxnr(map.get("gxnr"));
			record.setGy(map.get("gy"));
			record.setHgbz(map.get("hgbz"));
			record.setFsff(map.get("fsff"));
			record.setGqj(map.get("gqj"));
			record.setDegs(map.get("degs"));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return record;
	}

}
